#ifndef TOOLS_TCPSERVER_INTERFACE_H_
#define TOOLS_TCPSERVER_INTERFACE_H_

#include <stdint.h>
#include <stddef.h>
#include "ITcpListener.h"
#include "Global.h"
#include <memory>

namespace Tool
{

/*
 * OCP（Windows）和 epoll（Linux）作为主流的 IO 多路复用机制，核心工作流程确实包含三个关键环节：绑定 socket、事件通知、循环监控，三者缺一不可。
 * 将创建的socket套字节于复用机制绑定，实时获取事件即可
*/
class ExportAPI TcpServer
{
public:
    TcpServer(ITcpListener *listener, int threadCount = 2);
    ~TcpServer();
    bool Start(const char* bindAddress, uint16_t port);
    bool Stop();
    bool Send(const char* address, uint16_t port, const void* buffer, size_t length);
    bool Disconnect(const char* address, uint16_t port);

private:
    void AcceptLoop();
#ifdef _WIN32
    void EventWorkerThread();
#else
    void EpollWorkerThread();
    void HandleReadEvent(int clientSocket);
    void HandleWriteEvent(int clientSocket);
    void HandleErrorEvent(int clientSocket);
#endif

    struct IOContext;
    struct ITcpServerPrivater;
    struct MessageBuffer;
    enum class IOType;
    std::unique_ptr<ITcpServerPrivater> m_privater;
};
}
#endif
